package com.ca.ridersclubapplication.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.ca.ridersclubapplication.ClubActivity;
import com.ca.ridersclubapplication.data.bean.BannerInfo;
import com.ca.ridersclubapplication.util.ImageCacheUtils;
import com.youth.banner.adapter.BannerAdapter;
import com.youth.banner.holder.BannerImageHolder;

import java.util.ArrayList;
import java.util.List;

public class BannerImageAdapter extends BannerAdapter<BannerInfo, BannerImageHolder> {

    private Activity activity;
    private List<BannerInfo> bannerInfoList;

    public BannerImageAdapter(List<BannerInfo> datas) {
        super(datas);
        this.bannerInfoList = datas == null ? new ArrayList<>() : datas;
    }

    @Override
    public BannerImageHolder onCreateHolder(ViewGroup parent, int viewType) {

        ImageView imageView = new ImageView(parent.getContext());
        imageView.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        return new BannerImageHolder(imageView);
    }

    @Override
    public void onBindView(BannerImageHolder holder, BannerInfo data, int position, int size) {
        if (activity != null) {
            ImageCacheUtils.loadRoundImage(activity, data.getImageUrl(), holder.imageView, 30);
            holder.imageView.setOnClickListener(v -> {
                Intent intent = new Intent(activity, ClubActivity.class);
                intent.putExtra("clubId", data.getClubId());
                activity.startActivity(intent);
            });
        }
    }

    public void setActivity(Activity activity) {
        this.activity = activity;
        setBannerInfoList(bannerInfoList);
    }

    public List<BannerInfo> getBannerInfoList() {
        return bannerInfoList;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setBannerInfoList(List<BannerInfo> bannerInfoList) {
        this.bannerInfoList = bannerInfoList == null ? new ArrayList<>() : bannerInfoList;
        notifyDataSetChanged();
    }
}
